package com.c6906.vdam.das.controller;

import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateRange;
import cn.hutool.core.date.DateUtil;
import com.c6906.vdam.das.model.dto.ResultInfo;
import com.c6906.vdam.das.model.dto.ResultUtil;
import com.c6906.vdam.das.service.AlarmService;
import com.c6906.vdam.das.service.AreaCheckService;
import io.swagger.annotations.Api;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Date;

/**
 * @Author : TAOHUIQI @Date: 2024/6/15 20:49 @Description:
 */
@RestController
@RequestMapping({"/warn"})
@Api(description = "预警数据Controller")
public class WarnController {

    private static final Logger logger = LoggerFactory.getLogger(WarnController.class);

    @Resource
    AlarmService alarmService;

    @Resource
    AreaCheckService areaCheckService;

    @GetMapping("/initArea")
    public ResultInfo initArea() {
        logger.info("【初始化围栏信息】开始执行......");
        areaCheckService.initFunctionalArea();
        logger.info("【初始化围栏信息】结束执行......");
        return ResultUtil.getSuccessResult("ok");
    }


    @GetMapping("/analyse1")
    public ResultInfo illegalParkAnalyse(@RequestParam("startDate") String startDate, @RequestParam("endDate") String endDate) {
        DateRange range = DateUtil.range(DateUtil.parse(startDate), DateUtil.parse(endDate), DateField.DAY_OF_YEAR);
        logger.info("【违规停车】开始执行:startDate-{},endDate-{}......", startDate, endDate);
        range.forEach(dateTime -> alarmService.analyzeIllegalParking(dateTime.toDateStr()));
        logger.info("【违规停车】结束执行......");
        return ResultUtil.getSuccessResult("ok");
    }

    @GetMapping("/analyse2")
    public ResultInfo noTaskAnalyse(@RequestParam("startDate") String startDate, @RequestParam("endDate") String endDate) {
        DateRange range = DateUtil.range(DateUtil.parse(startDate), DateUtil.parse(endDate), DateField.DAY_OF_YEAR);
        logger.info("【公车私用-无任务出车】开始执行:startDate-{},endDate-{}......", startDate, endDate);
        range.forEach(dateTime -> alarmService.analyzeNoTask(dateTime.toDateStr()));
        logger.info("【公车私用-无任务出车】结束执行......");
        return ResultUtil.getSuccessResult("ok");
    }

    @GetMapping("/analyse3")
    public ResultInfo unauthTaskAnalyse(@RequestParam("startDate") String startDate, @RequestParam("endDate") String endDate) {
        DateRange range = DateUtil.range(DateUtil.parse(startDate), DateUtil.parse(endDate), DateField.DAY_OF_YEAR);
        logger.info("【擅自改变出行线路】开始执行:startDate-{},endDate-{}......", startDate, endDate);
        range.forEach(dateTime -> alarmService.analyzeUnauthTask(dateTime.toDateStr()));
        logger.info("【擅自改变出行线路】结束执行......");
        return ResultUtil.getSuccessResult("ok");
    }

    @GetMapping("/analyse4")
    public ResultInfo illegalPlaceAnalyse(@RequestParam("startDate") String startDate, @RequestParam("endDate") String endDate) {
        DateRange range = DateUtil.range(DateUtil.parse(startDate), DateUtil.parse(endDate), DateField.DAY_OF_YEAR);
        logger.info("【违规出入特定场所】开始执行:startDate-{},endDate-{}......", startDate, endDate);
        range.forEach(dateTime -> alarmService.analyzeIllegalPlace(dateTime.toDateStr()));
        logger.info("【违规出入特定场所】结束执行......");
        return ResultUtil.getSuccessResult("ok");
    }

    @GetMapping("/analyseMonth")
    public ResultInfo warnOfMonth(@RequestParam("month") String month) {
        Date date = DateUtil.parse(month, "yyyy-MM");
        Date start = DateUtil.beginOfMonth(date);
        Date end = DateUtil.endOfMonth(date);
        logger.info("【公车预警汇总】开始执行:month-{}......", month);
        alarmService.analyzeWarnMonth(start, end);
        logger.info("【公车预警汇总】结束执行:month-{}......", month);
        return ResultUtil.getSuccessResult("ok");
    }
}
